package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 2025版发动机数据格式 (0x04)
 * 参考GB/T 32960.3-2025标准表20
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class EngineData {
    @Field(length = 2, desc = "曲轴转速:有效范围0~60000(表示0r/min~60000r/min)")
    private int crankshaftSpeed;
    
    // ==================== 传输值与真实值转换方法 ====================
    
    /**
     * 获取曲轴转速 (r/min)
     * @return 转速，异常返回-1，无效返回-2
     */
    public int getCrankshaftSpeed() {
        if (crankshaftSpeed == 0xFFFE) return -1; // 异常
        if (crankshaftSpeed == 0xFFFF) return -2; // 无效
        return crankshaftSpeed; // 传输值 = 值(r/min)
    }
    
    /**
     * 设置曲轴转速 (r/min)
     * @param speed 转速值，范围0~60000 r/min
     */
    public void setCrankshaftSpeed(int speed) {
        if (speed < 0 || speed > 60000) {
            this.crankshaftSpeed = 0xFFFF; // 无效值
        } else {
            this.crankshaftSpeed = speed; // 值 = 传输值
        }
    }
    
    /**
     * 检查曲轴转速是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isCrankshaftSpeedValid() {
        return crankshaftSpeed != 0xFFFE && crankshaftSpeed != 0xFFFF;
    }
}